/*
 * @Author: wuqingchun
 * @Date: 2024-09-23 14:30:20
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-09-23 15:24:28
 */
#ifndef __CHAT_REDIS_PUBLISHER_H__
#define __CHAT_REDIS_PUBLISHER_H__

#include <thread>

#include "../../deps/hiredis/async.h"
#include "../../deps/hiredis/adapters/libevent.h"

class CChatRedisPublisher
{
public:
    CChatRedisPublisher();
    ~CChatRedisPublisher();

    int32_t Init();
    void Exit();

    int32_t Connect(const char* pcHost, uint16_t usPort);
    int32_t Publish(const char* pcChannel, const char* pcMessage);

private:
    static void __OnConnect(const redisAsyncContext* pstRedisCtx, int iStatus);
    static void __OnDisconnect(const redisAsyncContext* pstRedisCtx, int iStatus);
    static void __OnCommandReply(struct redisAsyncContext* pstRedisCtx, void* pReply, void* pPrivate);

    // 事件分发线程函数
    static void *event_thread(void *data);

    void __ThreadEventFunc();
private:
    // libevent事件对象
    event_base* m_pstEventBase = nullptr;
    std::thread m_objEventThread;
    
    //  hiredis 异步连接上下文
    redisAsyncContext* m_pstRedisCtx = nullptr;
};

#endif //__CHAT_REDIS_PUBLISHER_H__